<?php
/**
 * 栏目
 */
class CategoryController extends CommonController{
	private $db;
	private $cid;
	public function __auto(){
		$this->db = K('category');
		$this->cid = Q('cid',0,'intval');
		if(!isset($this->cid)){
			$this->error('该栏目不存在！','index');
		}
	}
	/**
	 * 显示分类
	 */
	public function index(){
		$categories = $this->db->all();
		$categories = Data::tree($categories,'catname');
		$this->assign('categories',$categories);
		$this->display();
	}
	/**
	 * 添加分类
	 */
	public function add(){
		if(IS_POST){
			if($this->db->addCategory()){
				$this->success('添加类目成功！','index');
			}else{
				$this->error($this->db->error);
			}
		}else{
			$pid = Q('gtid');
			if(!is_null($pid)){
				$type = $this->db->table('goods_type')->find($pid);
				$this->assign('type',$type);
			}
			$types = $this->db->table('goods_type')->all();
			$categories = $this->db->all();
			$categories = Data::tree($categories,'catname');
			
			$this->assign('categories',$categories);
			$this->assign('types',$types);
			$this->display();
		}
	}
	/**
	 * 编辑分类
	 */
	public function edit(){
		if(IS_POST){
			if($this->db->editCategory()){
				$this->success('修改栏目成功！','index');
			}else{
				$this->error($this->db->error);
			}
		}else{
			$pid = Q('gtid');
			if(!is_null($pid)){
				$goods_type = $this->db->table('goods_type')->find($pid);
				$this->assign('goods_type',$goods_type);
			}
			
			$cid =Q('cid',0,'intval');//栏目cid
			$field = $this->db->find($cid);//当前编辑栏目信息
			$type = $this->db->table('goods_type')->all();
			$categories = $this->db->all();
			$categories = Data::tree($categories,'catname');
			foreach($categories as $k=>$v){
				if(Data::isChild($categories, $v['cid'], $field['cid'])){
					$categories[$k]['disabled'] = 'disabled=""'; 
				}
			}
			$this->assign('field',$field);
			$this->assign('categories',$categories);
			$this->assign('type',$type);
			$this->display();
		}
	}
	/**
	 * 删除分类
	 */
	public function del(){
		$map['cid'] = $this->cid;
		if($this->db->find($map)){
			$this->error('请先删除子栏目');
		}
	}
	
	
}

















?>